<template>
	<view class="header-bg-image header-bg">
		<view style="height: 50rpx;"></view>
		<view class="padding-30">
			<view class="margin-30-0 flex-around" @click="$tools.navigateBack()">
				<u-icon name="arrow-left" color="#333333" size="20"></u-icon>
				<view class="fontSize-18 color-000 flex1 text-center">手机号修改</view>
				<view style="width: 40rpx;"></view>
			</view>
			<view class="margin-top150 input-name">
				<input type="number" maxlength="11" v-model="mobile" placeholder="请输入手机号"/>
			</view>
			<view class="margin-top30 input-name flex-between">
				<input class="flex1" type="text" v-model="captcha" placeholder="请输入验证码"/>
				<view class="fontSize-15 color-704234 margin-right30" style="flex-shrink: 0;"
				@click="getCode">
					{{tips?tips:'获取验证码'}}
				</view>
			</view>
			<view class="margin-top150">
				<view class="btn30-967C5E-15ffffff flex-center">保存</view>
			</view>
		</view>
		
		<!-- 验证码 -->
		<view>
			<u-toast ref="uToast"></u-toast>
			<u-code :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange"></u-code>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mobile:'',
				captcha:'',
				// 验证码
				tips: '',
				// 验证码分钟
				seconds:30,
				min:0,
				time:'',
			}
		},
		onShow() {
			this.getUserInfo()
		},
		methods:{
			// 获取个人信息
			async getUserInfo() {
				let res = await this.$http.post(this.$api.user_info, {}, {
					token: true
				})
				this.mobile = res.data.mobile
			},
			// 验证码
			codeChange(text) {
				this.tips = text;
			},
			// 获取验证码
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					if(!this.mobile){
						return this.$tools.toast('请填写手机号')
					}
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					let that = this
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						that.$refs.uCode.start();
						let res = that.$http.post(that.$api.send,{mobile:that.mobile,event:'login'},{token:true})
						if(res.code == 1){
							this.$tools.toast(res.msg)
						}
					}, 1000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				uni.$u.toast('倒计时结束');
			},
			start() {
				uni.$u.toast('倒计时开始');
			}
		}
	}
</script>

<style lang="scss" scoped>
	.input-name {
		border: 2rpx solid #967C5E;
		padding: 30rpx;
		background-color: #ffffff;
		border-radius: 60rpx;
		font-size: 15px;
		color: #000000;
	}
</style>